package com.hs.controller;

import com.hs.entity.CourseCollect;
import com.hs.service.CourseCollectService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("coursecollect")
public class CourseCollectController {
    @Autowired
    private CourseCollectService courseCollectService;
    @PostMapping
    public R addCourseCollect(@RequestBody CourseCollect courseCollect) {
        int i = courseCollectService.addCourseCollect(courseCollect);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @PutMapping
    public R updateCourseCollect(@RequestBody CourseCollect courseCollect) {
        int i = courseCollectService.updateCourseCollect(courseCollect);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @DeleteMapping("{id}")
    public R deleteCourseCollect(@PathVariable("id") int id) {
        int i = courseCollectService.deleteCourseCollect(id);
        if (i > 0) {
            return R.ok();
        }
        return R.fail("失败");
    }
    @GetMapping("{id}")
    public R getCourseCollectById(@PathVariable("id") int id) {
        List<CourseCollect> courseCollectById = courseCollectService.getCourseCollectById(id);
        return R.ok(courseCollectById);
    }
    @GetMapping("/add/{cid}/{uid}")
    public R addCollect(@PathVariable("cid") int cid,@PathVariable("uid") int uid) {
        int row = courseCollectService.collectCourse(cid, uid);
        if (row > 0) {
            return R.ok("收藏成功，获得10积分");
        } else {
            return R.fail("收藏失败，可能已收藏");
        }
    }
    /**
     * 取消收藏
     */
    @DeleteMapping("/canl/{cid}/{uid}")
    public R cancelCollect(@PathVariable("cid") int cid,@PathVariable("uid") int uid) {
        int row = courseCollectService.cancelCollect(cid, uid);
        if (row > 0) {
            return R.ok("取消收藏成功");
        } else {
            return R.fail("取消收藏失败");
        }
    }
    /**
     * 检查是否收藏
     */
    @GetMapping("/check/{cid}/{uid}")
    public R checkCollect(@PathVariable("cid") int cid,@PathVariable("uid") int uid) {
        boolean isCollected = courseCollectService.isCollected(cid, uid);
        return R.ok(isCollected);
    }
}
